#include "led.h"

#define __delay_ms		wk_delay_ms
static void beep_trig_init(void);

/**
  * @brief LED GPIO Initialization Function
  * @param None
  * @retval None
  */
void led_gpio_init(void)
{
	gpio_init_type gpio_init_struct;
	
	/* enable the led clock */
	crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(LED_RUN_GPIO_CRM_CLK, TRUE);	
	crm_periph_clock_enable(BEEP_GPIO_CRM_CLK, TRUE);	
	
	/* configure the led gpio */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type	= GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
	gpio_init_struct.gpio_pins = LED_RUN_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(LED_RUN_GPIO, &gpio_init_struct);
	LED_RUN_OFF;

	gpio_init_struct.gpio_pins = BEEP_PIN;
	gpio_init(BEEP_GPIO, &gpio_init_struct);
	BEEP_OFF;

	beep_trig_init();
}

/**
  * @brief LED GPIO Initialization Function
  * @param LEDx  LED is number;  state: LED_ON,LED_OFF,LED_TOGGLE;
  * @retval None
  */
void led_state_set(uint8_t LEDx,LEDState_TypeDef state)
{
  if(state==LED_OFF)
  {
    if(LEDx & LED1)            
      LED_RUN_OFF;

    if(LEDx & BEEP)
      BEEP_OFF;	
  }
  else if(state==LED_ON)
  {
    if(LEDx & LED1)
      LED_RUN_ON;
    
    if(LEDx & BEEP)
      BEEP_ON;	    
  }
  else
  {
    if(LEDx & LED1)
      LED_RUN_TOGGLE;
	
    if(LEDx & BEEP)
      BEEP_TOGGLE;		
  }
}

static uint8_t beep_en = 0;


void beep_onoff(uint16_t cnt, uint16_t on_time, uint16_t off_time)
{
    if(beep_en == 0)
        return;
        
	for(; cnt>0; cnt--)
	{
		led_state_set(BEEP, LED_ON);
		__delay_ms(on_time);
		led_state_set(BEEP, LED_OFF);
		__delay_ms(off_time);
	}
}


typedef struct
{
	uint16_t tick;
	uint16_t on_tick;
	uint16_t off_tick;
	uint8_t beep_ctrl_en;
	uint8_t beep_sta;
	
	void (*beep_ctrl)(u8);

}SYS_BEEP;



SYS_BEEP enc_beep;

/**
 * @description: ·äÃùÆ÷×Ô¶¯Í£Ö¹¿ØÖÆ£¬systick£¬1ms ISR
 * @return {*}
 */
void beep_tick_handle(void)
{
    if(beep_en == 0)
        return;

	if(enc_beep.beep_ctrl_en == TRUE)
	{
		if(enc_beep.beep_sta == OFF)
		{
			enc_beep.beep_sta = ON;
			enc_beep.beep_ctrl(enc_beep.beep_sta);
			enc_beep.tick ++;
		}
		else
		{
			enc_beep.tick ++;
			if(enc_beep.tick >= enc_beep.on_tick)
			{
				enc_beep.beep_sta = OFF;
				enc_beep.beep_ctrl(enc_beep.beep_sta);
				
				enc_beep.beep_ctrl_en = FALSE;
			}
		}
	}
	else
	{
		enc_beep.tick = 0;
	}
}


static void beep_ctrl_onoff(u8 sta)
{
    if(beep_en == 0)
        return;
    
	if(sta)
	{
		BEEP_ON;
	}
	else
	{
		BEEP_OFF;
	}
}

static void beep_trig_init(void)
{
	enc_beep.tick = 0;
	enc_beep.on_tick = 50;
	enc_beep.beep_sta = OFF;
	enc_beep.beep_ctrl = beep_ctrl_onoff;
	
	enc_beep.beep_ctrl_en = FALSE;
}

void beep_key_trig(uint16_t cnt)
{
    if(beep_en == 0)
        return;
	if(enc_beep.beep_ctrl_en != TRUE)
	{
		enc_beep.tick = 0;
		enc_beep.beep_ctrl_en = TRUE;
		enc_beep.on_tick = cnt;
		enc_beep.beep_sta = OFF;
	}
}

void beep_reset_cnt(void)
{
	enc_beep.tick = 0;
}

void beep_ctrl_enable(u8 sta)
{
    if(sta)
    {
        beep_en = 1;
    }
    else
    {
        beep_en = 0;
    }
}










